package  katris.selenium.tests.applogic1;

import static org.openqa.selenium.support.ui.ExpectedConditions.*;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;





import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import  katris.selenium.tests.applogic.FilmHelper;
import  katris.selenium.tests.model.Film;


public class FilmHelper1 extends DriverBasedHelper implements FilmHelper {

private ApplicationManager1 manager;
	
  public FilmHelper1(ApplicationManager1 manager) {
    super(manager.getWebDriver());
    this.manager = manager;
  }

  @Override
  public void create(Film film) {
  manager.getNavigationHelper().openNewDescription();
  
  WebElement titleInsert = wait.until(elementToBeClickable(By.xpath("//input[@name='name']")));
  titleInsert.click();
  titleInsert.sendKeys(Keys.CONTROL + "a");
  titleInsert.sendKeys(Keys.DELETE);
  titleInsert.sendKeys(film.getTitle());

  WebElement yearInsert = wait.until(elementToBeClickable(By.xpath("//input[@name='year']")));
  yearInsert.click();
  yearInsert.sendKeys(Keys.CONTROL + "a");
  yearInsert.sendKeys(Keys.DELETE);
  yearInsert.sendKeys(film.getYear());
  WebElement saveMovie = wait.until(elementToBeClickable(By.xpath("//input[@id='submit']")));
  saveMovie.click();
  

  }

  @Override
  public void delete(Film film) {
	  manager.getNavigationHelper().openShortInfo(film);
      WebElement movie = wait.until(elementToBeClickable(By.xpath("//img[@title='Remove']")));
      movie.click();
	    wait.until(alertIsPresent()).accept();
	    
  }

  @Override
  public void search(Film basicFilm, Film film) {
	  
	  // clean previous searching. It necessary for running two tests (positive and negative) in a row 
	  WebElement search= new WebDriverWait(driver,20).until(elementToBeClickable(By.xpath("//input[@id='q']")));
      search.sendKeys(Keys.CONTROL + "a");
      search.sendKeys(Keys.DELETE);
      search.sendKeys(Keys.RETURN);
	  
	  WebElement filmName = new WebDriverWait(driver,10).until(visibilityOfElementLocated(By.xpath("//div[@title='" + basicFilm.getTitle() + "']")));

      search.sendKeys(film.getTitle());

      search.sendKeys(Keys.RETURN);
      
      wait.until(ExpectedConditions.stalenessOf(filmName));
    
  }
  
  @Override
  public boolean checkPresence(Film film) {
   // return driver.findElements(By.xpath("//div[@title='" + film.getTitle() + "']")).size() > 0;
	  
	  List<WebElement> listOfFilms = wait.until(visibilityOfAllElementsLocatedBy(By.xpath("//div[@title='" + film.getTitle() + "']")));
	  
      int numberOfFilms = listOfFilms.size();
      
      System.out.println("Number of found Films = " + numberOfFilms);

      if (numberOfFilms!= 0) {

          if (numberOfFilms > 1) {
     
              return true;}
          else {
              return true;}
      }
      else {return false;}
  }
  
  @Override
  public boolean checkNotPresence(Film film) {
  	  
		wait.until(invisibilityOfElementLocated(By.xpath("//div[@title='" + film.getTitle() + "']")));
    	  return true;
  }
  
  @Override
  public boolean checkShortInfo(Film film) {
	  
	  //return driver.findElement(By.xpath("//div[@class='maininfo_full']")).getText().contains(film.getTitle())&&driver.findElement(By.xpath("//div[@class='maininfo_full']")).getText().contains(film.getYear());
	 
      boolean check = true;
	  WebElement movie = wait.until(elementToBeClickable(By.xpath("//div[@class='maininfo_full']")));
      String text = movie.getText();

      if (text.contains(film.getTitle())){
    	  System.out.println("Movie Name is correct");
          }
      else {
      check = false;}

      if (text.contains(film.getYear())){
    	  System.out.println("Movie Year is correct");
      }
      else {
          check = false;}

      if (check == true)  {
          return true;
      }
     else {

          return false;
      }
  }

  public boolean checkIfRequiredDataIsSet(){
	  WebElement field1 = wait.until(presenceOfElementLocated(By.xpath("//label[@for='name']")));
      if (field1.isDisplayed()) {
         return false;
      }

    WebElement field2 = wait.until(presenceOfElementLocated(By.xpath("//label[@for='year']")));
    if (field2.isDisplayed()) {
        return false;
    }
    else {
        return true;  }
  } 

}
